package com.hui.dao.impl;

import com.hui.bean.ClassBean;
import com.hui.dao.IClassDao;
import com.hui.util.DBUtils;
import org.apache.ibatis.session.SqlSession;

import java.sql.*;
import java.util.List;

/**
 * 班级相关dao实现层
 *
 */
public  class ClassDaoImpl implements IClassDao {

    @Override
    public List<ClassBean> getAllClasses() throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IClassDao mapper = session.getMapper(IClassDao.class);
        List<ClassBean> classes = mapper.getAllClasses();

        return classes;
    }


    /**
     * 添加一个班级
     * @param classBean
     * @return
     * @throws SQLException
     */
    @Override
    public int saveClass(ClassBean classBean) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IClassDao mapper = session.getMapper(IClassDao.class);
        int result = mapper.saveClass(classBean);
        session.commit();
        session.close();
        return result;
        }

    /**
     * 通过id删除一个班级
     * @param classId
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteClassById(int classId) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IClassDao mapper = session.getMapper(IClassDao.class);
        int result = mapper.deleteClassById(classId);
        session.commit();
        session.close();
        return result;

      }

    /**
     * 通过班级名字得到对应的班级
     * @param className  班级名称
     * @return 对应的班级
     * @throws SQLException
     */
    @Override
    public ClassBean getClassByName(String className) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IClassDao mapper = session.getMapper(IClassDao.class);
        ClassBean classBean = mapper.getClassByName(className);
        session.close();
        return classBean;
       }


}
